<?php

declare(strict_types=1);

namespace Api\InterfaceApi\v1;

use Api\Request\v1\SiteAddRequest;
use App\Site\Model\SiteInfo;
use Mine\Annotation\Api\MApi;
use Mine\MineResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Ramsey\Uuid\Uuid;

/**
 * 代理商租户接口
 */
class SiteApi
{
    protected MineResponse $response;

    /**
     * DemoApi constructor.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __construct()
    {
        $this->response = container()->get(MineResponse::class);
    }

    /**
     * 获取用户列表接口.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'site.add', name: '添加一个租户', description: '添加一个租户', appId: '7ce297a4e4', authMode: 1, groupId: 3)]
    public function add(SiteAddRequest $req): ResponseInterface
    {
        // 检查是否存在
        $site = SiteInfo::where('app_id', $req->app_id)->where('custom_tag', $req->custom_tag)->first();
        if (!$site) {
            $site = new SiteInfo();
            $site->app_id = $req->app_id;
            $site->uuid = Uuid::uuid3(env('UUID_NS'), $req->custom_tag);
            $site->custom_tag = $req->custom_tag;
        }
        $site->save();

        return $this->response->success('请求成功', $site->toArray());
    }
}
